package com.xjj.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xjj.entity.Manager;
import com.xjj.entity.Photo;
import org.apache.ibatis.annotations.*;

import java.util.List;


public interface PhotoMapper extends BaseMapper<Photo> {
    //根据发送者(班主任)id查询所有相册
    @Select("select * from xjj_photo where manager_id = #{id} order by time desc")
    @Results({
            @Result(column = "id",property = "id"),
            //查询相册中的图片
            @Result(column = "id",property = "img",one=@One(select = "com.xjj.mapper.PhotoMapper.getImgByPhotoId"))
    })
    List<Photo> getAllPhotoByMangerId(int id);
    //根据相册id查询图片
    @Select("select img from xjj_photo_png where photo_id=#{id}")
    List<String> getImgByPhotoId(int id);

    //根据班级id查看相册
    @Select("select * from xjj_klass xk,xjj_photo")
    List<Photo> getPhotoByKlassId(String klassId);

    //根据相册id删除相册
    @Delete("delete from xjj_photo where xjj_photo.id = #{id}")
    void deletePhoto(int id);
    //根据相册id删除相册图片
    @Delete(("delete from xjj_photo_png where xjj_photo_png.photo_id = #{id}"))
    void deletePhotoPngByPhotoId(int id);

    //发布相册
    @Insert("insert into xjj_photo values(default,#{managerId},#{time},#{content},0,0)")
    @Options(useGeneratedKeys = true,keyColumn = "id",keyProperty = "id")
    void addPhoto(Photo photo);
    //插入图片
    @Insert("insert into xjj_photo_png values(default,#{photoId},#{img})")
    void addPhotoPng(@Param("photoId")int photoId,@Param("img") String img);

    //模糊查询相册
    @Select("select * from xjj_photo where manager_id = #{id} and time like concat('%',#{content},'%') or content like concat('%',#{content},'%') order by time desc")
    @Results({
            @Result(column = "id",property = "id"),
            //查询相册中的图片
            @Result(column = "id",property = "img",one=@One(select = "com.xjj.mapper.PhotoMapper.getImgByPhotoId"))
    })
    List<Photo> getByTimeOrContent(@Param("id")int managerId,@Param("content")String content);

    //家长点赞攻能
    @Update("update xjj_photo set like_num = like_num+1 where id = #{id}")
    void like(int id);
    //消赞
    @Update("update xjj_photo set like_num = like_num-1 where id = #{id}")
    void unlike(int id);

    //查看相册详情
    @Select("select * from xjj_photo where id = #{photoId}")
    @Results({
            @Result(column = "id",property = "id"),
            //查询相册中的图片
            @Result(column = "id",property = "img",one=@One(select = "com.xjj.mapper.PhotoMapper.getImgByPhotoId"))
    })
    Photo getByPhotoId(int photoId);

    //根据学生ID查询班主任
    @Select("select master_id from xjj_klass where id = " +
            "(select klass_id from xjj_student where id = {studentId})")
    int getManagerId(int studentId);

}
